package com.alameda.common.config.job;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 定时任务中间件 xxl-job 配置类
 *
 * @author lv
 * @date 2023/2/25
 */
@Configuration
@PropertySource("classpath:job.properties")
public class XxlJobConfig {
    /**
     * admin 服务地址
     */
    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    /**
     * 访问令牌
     */
    @Value("${xxl.job.accessToken}")
    private String accessToken;

    /**
     * 执行器名称
     */
    @Value("${xxl.job.executor.appname}")
    private String appname;

    /**
     * 执行器地址
     */
    @Value("${xxl.job.executor.address}")
    private String address;

    /**
     * 执行器IP
     */
    @Value("${xxl.job.executor.ip}")
    private String ip;

    /**
     * 执行器端口
     */
    @Value("${xxl.job.executor.port}")
    private int port;

    /**
     * 执行器日志路径
     */
    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    /**
     * 日志保留天数
     */
    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    /**
     * 执行对象以单例模式注入Spring容器  初始实例 结束销毁
     *
     * @return
     */
    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSpringExecutor xxlJobExecutor() {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setAddress(address);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSpringExecutor;
    }
}